package org.jjj.market.controller;

import org.jjj.market.dto.ClueActivityDto;
import org.jjj.market.dto.ClueBusinessDto;
import org.jjj.market.service.IClueService;
import org.jjj.market.domain.Clue;
import org.jjj.market.query.ClueQuery;
import org.jjj.market.vo.ShowPieChartVO;
import org.jjj.org.basic.utils.ResultAjax ;
import org.jjj.org.basic.utils.PageList ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clue")
public class ClueController {
    @Autowired
    public IClueService clueService;


    /**
     * 保存和修改公用的
     * @param clue  传递的实体
     * @return ResultAjax转换结果
     */
    @PutMapping
    public ResultAjax addOrUpdate(@RequestBody Clue clue){
        try {
            if( clue.getId()!=null)
                clueService.update(clue);
            else
                clueService.add(clue);
            return ResultAjax.me();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public ResultAjax delete(@PathVariable("id") Long id){
        try {
            clueService.deleteById(id);
            return ResultAjax.me();
        } catch (Exception e) {
        e.printStackTrace();
            return ResultAjax.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public ResultAjax get(@PathVariable("id")Long id)
    {
        try {
            Clue clue = clueService.selectById(id);
            return ResultAjax.me().setResult(clue);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public ResultAjax list(){

        try {
            List< Clue> list = clueService.selectAll();
            return ResultAjax.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public ResultAjax json(@RequestBody ClueQuery query)
    {
        try {
            PageList<Clue> pageList = clueService.queryPage(query);
            return ResultAjax.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    @PatchMapping
    public ResultAjax batchRemove(@RequestBody List<Long> ids){
        try {
            clueService.batchRemove(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("后端批量删除失败"+e.getMessage());
        }
        return ResultAjax.me();
    }
    /**
     * 线索绑定活动之后向中间关联表保存数据
     */
    @PostMapping("/activity")
    public ResultAjax ClueBindActivity(@RequestBody ClueActivityDto dto){
        try {
            clueService.clueBindActivities(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("后端绑定活动失败"+e.getMessage());
        }
        return ResultAjax.me();
    }
    @PostMapping("/assign")
    public ResultAjax ClueAssignSave(@RequestBody Clue clue){
        try {
            clueService.assignClue(clue);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("后端保存分配线索失败"+e.getMessage());
        }
        return ResultAjax.me();
    }
    @PostMapping("/business")
    public ResultAjax BusinessSubmit(@RequestBody ClueBusinessDto dto){
        try {
            clueService.clueChangeBusiness(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("后端保存商机转化数据失败"+e.getMessage());
        }
        return ResultAjax.me();
    }

    /**
     * 查出所有线索来源传递到前端
     * @return
     */
    @GetMapping("/pie")
    public ResultAjax getPieChartData(){

        try {
            List<ShowPieChartVO> pieChartData = clueService.getPieChartData();
            return ResultAjax.me().setResult(pieChartData);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAjax.me().setSuccess(false).setMessage("获取所有线索来源失败！"+e.getMessage());
        }
    }
}
